package com.yhh.member.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhh.common.base.ret.ApiResult;
import com.yhh.common.base.ret.controller.BaseController;
import com.yhh.common.entity.member.dto.UserDto;
import com.yhh.common.entity.member.model.User;
import com.yhh.common.entity.member.view.UserView;
import com.yhh.common.exception.BusinessException;
import com.yhh.common.utils.FieldsFilter;
import com.yhh.member.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 用户模块
 */
@Api(tags = "用户服务API")
@RestController
@RequestMapping("member/api/v2")
@Slf4j
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    @GetMapping("getCurrUser")
    @ApiOperation(value = "根据当前登录的用户信息")
    public ApiResult getCurrUser(){
        User currentUser = super.getCurrentUser();
        return ApiResult.success().put("user",currentUser);
    }
    @GetMapping("getUserById")
    @ApiOperation(value = "根据ID获取用户信息")
    public ApiResult getUserById(Long id){
        if(id == 0){
            throw new BusinessException("请输入正确的ID");
        }
        User user = userService.getById(id);
        return ApiResult.success().put("user", FieldsFilter.filter(user, Arrays.asList("name","sex","age")));
    }

    @GetMapping("getUserList")
    @ApiOperation(value = "获取用户信息列表")
    public ApiResult getUserList(UserDto dto){
        dto.setOrderby("age");
        IPage page = userService.getByPage(dto);
        List<UserView> list = new ArrayList<>();
        page.getRecords().forEach(user -> {
            list.add(new UserView((User)user));
        });
        page.setRecords(list);
        return ApiResult.success().put("page",
                page.setRecords(FieldsFilter.filter(page.getRecords(),Arrays.asList("name","strSex","age"))));
    }

}
